package com.study.demo_db_security_dynamic.service;

import com.study.demo_db_security_dynamic.bean.User;
import com.study.demo_db_security_dynamic.mapper.UserMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service;

/**
 * @ClassName: UserService
 * @Description:
 * @author: WS
 * @date 2020/12/31
 * @Version: 1.0
 **/
@Service
public class UserService implements UserDetailsService {
    @Autowired
    UserMapper mapper;
    @Override
    public UserDetails loadUserByUsername(String name) throws UsernameNotFoundException {
        User user=mapper.loadUserByUsername(name);
        if(user==null){
            throw new UsernameNotFoundException("用户名不存在");
        }
        user.setRoles(mapper.getUserRolesById(user.getId()));
        return user;
    }
}
